package maps.apis;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class Maps_apiActivity extends MapActivity {

	MapView mapView;
	MapController mc;
	GeoPoint p;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// hien thi control + - de zoom ban do (1)
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		// enable Street view by default
		mapView.setStreetView(true);

		// enable to show Satellite view
		// mapView.setSatellite(true);

		// enable to show Traffic on map
		mapView.setTraffic(true);

		// end (1)

		mc = mapView.getController();
		
		
		// hiện bản đồ tại vị trí nào (2)
		
		LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, new LocationListener() {
			 	public void onStatusChanged(String provider, int status, Bundle extras) {
			 		// called when the provider status changes. Possible status: OUT_OF_SERVICE, TEMPORARILY_UNAVAILABLE or AVAILABLE.
			 	}
			 	public void onProviderEnabled(String provider) {
			 		//Toast.makeText(Maps_apiActivity.this, "GPS is enabled.",
		            //        Toast.LENGTH_LONG).show();
			 		// called when the provider is enabled by the user
			 	}
			 	public void onProviderDisabled(String provider) {
			 		//Toast.makeText(Maps_apiActivity.this, "GPS is disible.",
		            //        Toast.LENGTH_LONG).show();
			 		// called when the provider is disabled by the user, if it's already disabled, it's called immediately after requestLocationUpdates
			 	}

			 	public void onLocationChanged(Location location) {
			 		double latitute = location.getLatitude();
			 		double longitude = location.getLongitude();
			 		

			 		// do whatever you want with the coordinates
			 	
			 		p = new GeoPoint((int) (latitute * 1E6), (int) (longitude * 1E6));
					
					mc.animateTo(p);
					mc.setZoom(17); // 17
					mapView.invalidate();
			 	}
		 });
		
		
		
		
		
		
		
		String coordinates[] = { "10.7720", "106.6982" }; // vĩ độ - kinh độ 
															// (truong dh khtn)
		double lat = Double.parseDouble(coordinates[0]);
		double lng = Double.parseDouble(coordinates[1]);
		// connect to the GPS location service

		LocationManager lm = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);

		Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		if (loc != null)
		{
			lat = loc.getLatitude();
			lng = loc.getLongitude();		
		}
		

		p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
		
		mc.animateTo(p);
		mc.setZoom(17); // 17
		mapView.invalidate();
		// end (2)

		// kiem tra gps co mo chua neu chua mo bat hop thoai setting gps len (3)
		String provider = Settings.Secure.getString(getContentResolver(),
				Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

		if (!provider.equals("")) {
			// GPS Enabled
			Toast.makeText(Maps_apiActivity.this, "GPS Enabled: ON",
					Toast.LENGTH_LONG).show();

		} else {
			Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
			startActivity(intent);
		}

		// end (3)
// ctrl + /		
//		mapView.setOnTouchListener(new View.OnTouchListener() {
//			
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				// TODO Auto-generated method stub
//				int zoomlevel = mapView.getZoomLevel();
//				mc.setZoom(zoomlevel+2);
//
//				return true;
//			}
//		});
		
		
		
		
//		List<Overlay> mapOverlays = mapView.getOverlays();
//		Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
//		HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
//		GeoPoint point = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6));
//		OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
//		itemizedoverlay.addOverlay(overlayitem);
//		mapOverlays.add(0, itemizedoverlay);
		
		MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
		List<Overlay> list = mapView.getOverlays();
		list.add(myLocationOverlay);
		
		
}
	
	

	
	
protected class MyLocationOverlay extends com.google.android.maps.Overlay {
		
		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
		{
			Paint paint = new Paint();
			super.draw(canvas, mapView, shadow);
			// Converts lat/lng-Point to OUR coordinates on the screen.
			Point myScreenCoords = new Point();
			mapView.getProjection().toPixels(p, myScreenCoords);
			
			paint.setStrokeWidth(1);
			paint.setARGB(255, 255, 255, 255);
			paint.setStyle(Paint.Style.FILL_AND_STROKE);
			
			Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.sym_keyboard_shift);
			
			canvas.drawBitmap(bmp, myScreenCoords.x-25, myScreenCoords.y-40, paint);
			
			Paint paint2 = new Paint();
			paint2.setStrokeWidth(1);
			paint2.setARGB(255, 255, 0, 0);
			paint2.setStyle(Paint.Style.FILL_AND_STROKE);
			canvas.drawText("Kinh độ : " +p.getLongitudeE6() , myScreenCoords.x+20, myScreenCoords.y-15,paint2);
			canvas.drawText("Vĩ độ : "+p.getLatitudeE6(), myScreenCoords.x+20, myScreenCoords.y,paint2);
			return true;
		}
		public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---
            if (event.getAction() == 1) {                
                GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());
                
//                AlertDialog.Builder dialog = new AlertDialog.Builder(getBaseContext());
//        		dialog.setTitle("ABC");
//        		dialog.setMessage("BCD");
//        		dialog.show();  
                    Toast.makeText(getBaseContext(), 
                        p.getLatitudeE6() / 1E6 + "," + 
                        p.getLongitudeE6() /1E6 , 
                        Toast.LENGTH_SHORT).show();
            }                            
            return false;
        }
	}
//ham kem theo mapActivity
		protected boolean isRouteDisplayed() {
			return true;
		}
}